<template>
    <div>
        <el-button v-show="addBtnShow" class="addBtn" type="pramry" @click="addDialogVisible=true">添加{{addContent}}</el-button>
        <el-table :data="contents" style="width:100%;">

            <el-table-column show-overflow-tooltip prop="id" label="ID"/>
            <el-table-column show-overflow-tooltip prop="name" label="标题"/>
            <el-table-column show-overflow-tooltip prop="url" label="URL地址"/>
            <el-table-column show-overflow-tooltip prop="image" label="图片地址"/>
            <el-table-column prop="updateProduct" label="操作" width="120" fixed="right">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)"
                            icon="el-icon-delete"
                            circle
                    />
                </template>
            </el-table-column>
        </el-table>
        <el-dialog
            title="添加内容"
            :visible="addDialogVisible"
            width="60%"
            :before-close="handleClose"
        >
            <el-form ref="form" :model="form" label-width="80px">
                <el-form-item label="标题">
                    <el-input  v-model="form.name">

                    </el-input>
                </el-form-item>
                <el-form-item label="URL地址">
                    <el-input  v-model="form.url">

                    </el-input>
                </el-form-item>
                <el-form-item label="图片地址">
                    <el-input  v-model="form.image">


                    </el-input>
                </el-form-item>
            </el-form>
            <span>
                <el-button @click="addDialogVisible = false">取消</el-button>
                <el-button type="primary" @click="addDialogHandle">确定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        name:'ContentCategory',
        inject:['reload'],
        data(){
            return{
                contents:[],
                addBtnShow:false,
                addContent:'',
                addDialogVisible:false,
                form:{}
            }
        },
        methods:{
            handleClose(){
              this.addDialogVisible = false;
            },
            // 删除
            handleDelete(index,row){
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {

                    this.$api.getDeleteContentByIds({
                        id:row.id
                    }).then(res=>{
                        // console.log(res.data);
                        if (res.data.status === 200){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.reload();

                        }else {
                            this.$message({
                                type: 'error',
                                message: '删除失败!'
                            });
                            // alert('')
                        }
                    }).catch(error=>{
                        this.$message({
                            type: 'error',
                            message: '删除失败!'
                        });
                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            // 添加 确定按钮
            addDialogHandle(){
                this.$api.getInsertTbContent({
                    pid:this.advertisingData.pid,
                    name:this.form.name,
                    url:this.form.url,
                    image:this.form.image
                }).then(res=>{
                    if (res.data.status === 200){
                        this.addDialogVisible = false;
                        this.reload();
                    }else {
                        alert('添加失败')
                    }
                });
            }
        },
        props:{
            advertisingData:{
                type:Object,
                required:true,
            }
        },
        watch:{
            advertisingData(value,oldValue){
                this.addContent = value.name;
                if (value.name){
                    this.addBtnShow = true;
                }
                this.$api.getSelectTbContentAllByCategoryId({
                    id:value.pid
                }).then(res=>{
                    if (res.data.status === 200){
                        this.contents = res.data.data;
                    }else {
                        this.contents = [];
                    }
                })


            }
        },

    }
</script>

<style scoped>

</style>